package com.comast.eep.bridge.async;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.MDC;

import weblogic.javaee.TransactionTimeoutSeconds;

import com.comast.eep.bridge.EEPBridgeImpl;
import com.comcast.util.ObjectFactory;

@MessageDriven(mappedName = "eepQueue", name = "EEPBridge", activationConfig = {
		@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
		@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
})
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@TransactionTimeoutSeconds(10)
public class EEPBridge implements MessageListener {

    private static final Log logger = LogFactory.getLog(EEPBridge.class);

    public EEPBridge(){}
   
    
    private EEPBridgeImpl bridgeImpl = (EEPBridgeImpl)ObjectFactory.getBean("eep.bridge.impl");
    
	public void onMessage(Message msg) {
		long time = System.currentTimeMillis();
		try {
			String eventSourceKey = msg.getStringProperty("EVENTSOURCE_KEY");
			MDC.put("trackingId", eventSourceKey);
	
			String bcons = bridgeImpl.persist(msg);
			
			if(logger.isInfoEnabled()) {
				logger.info("CONS:"+bcons +"TNX: (" + (System.currentTimeMillis() - time ) + ") ms");
			}
			
		} catch (JMSException e) {
			logger.error("JMS_Exception, while consuming message from EEP Queue. ", e);
		} finally {
    		MDC.remove("trackingId");
    	}
		
	}

}
